/* 
 * File:   HashTableAdapter.cpp
 * Author: Christopher
 * 
 * Created on November 9, 2012, 8:46 PM
 */

#include <bitset>

#include "HashTableAdapter.h"

HashTableAdapter::HashTableAdapter() {

}

/**
 * Insert a Term object ptr into the hash table
 * @param node object ptr to insert into the hash table
 */
void HashTableAdapter::insert(Term* node) {
    pair<string, Term*> data(node->getTerm(), node);
    table.insert(data);
}

/**
 * Use the hash table to find a term
 * @param query string to match in the hash table
 * @return ptr to matched term or NULL if not found
 */
Term* HashTableAdapter::search(string query) {

    std::unordered_map<std::string, Term*>::const_iterator found = table.find(query);

    if (found == table.end())
        return NULL;
    //else
    //  std::cout << found->first << "found";

    return found->second;

}

/**
 * Output the hash table to an inverted index file
 * @param stream a stream reference to output to
 */
void HashTableAdapter::outputFile(ofstream &stream) {

    for (unordered_map<string, Term*>::iterator it = table.begin(); it != table.end(); ++it) {
        map<int,int>* documents = it->second->getDocumentOccurances();
        stream << it->first << "|";
        for (map<int,int>::iterator docit = documents->begin(); docit != documents->end(); ++docit) {
            if (docit != documents->begin()) {
                stream << ',';
            }
            stream << docit->first;
        }
    }

}

